// Package day_05_picture graph
// @Title day_05_picture
// @Description
// @Author  == 琳琳也叫王木木 ==  2021/8/31 17:28
// @Create  2021/8/31 17:28
package day_05_picture

type Point struct {
	Val      int
	In       int
	Out      int
	NextList []*Point
	EdgeList []*Edge
}

type Edge struct {
	Weight int
	From   Point
	To     Point
}

type Graph struct {
	NodeMap map[int]Point
	EdgeSet map[*Edge]bool // set
}

func CreateGraph(matrix [][]int) Graph {
	graph := Graph{}
	for i := 0; i < len(matrix); i++ {
		from := matrix[i][0]
		to := matrix[i][1]
		weight := matrix[i][2]
		if _, exists := graph.NodeMap[from]; exists != true {
			graph.NodeMap[from] = Point{}
		}
		if _, exists := graph.NodeMap[from]; exists != true {
			graph.NodeMap[from] = Point{}
		}
		fromPoint := graph.NodeMap[from]
		toPoint := graph.NodeMap[to]
		newEdge := Edge{
			From:   fromPoint,
			To:     toPoint,
			Weight: weight,
		}
		fromPoint.NextList = append(fromPoint.NextList, &toPoint)
		fromPoint.Out++
		toPoint.In++
		fromPoint.EdgeList = append(fromPoint.EdgeList, &newEdge)
		graph.EdgeSet[&newEdge] = true
	}
	return graph
}
